const fp = require('lodash/fp')
const { MayBe, Container } = require('./support')

// 练习1：使用fp.add(x,y)和fp.map(f,x)创建一个能让functor里的值增加的函数ex1
let maybe = MayBe.of([5, 6, 1])
let ex1 = (num) => {
    let a = fp.flowRight(fp.map, fp.add)
    return maybe.map(a(num))
}
console.log(ex1(1))




// 练习2：实现一个函数ex2，能够使用fp.first获取列表的第一个元素
let xs = Container.of(['do', 'ray', 'mi', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = () => {
    let first = undefined
    xs.map(value => {
        first = fp.first(value)
    })
    return first
}
console.log(ex2())




// 练习3：实现一个ex3，使用safeProp和fp.first找到user的名字的首字母
let safeProp = fp.curry(function (x, o) {
    return MayBe.of(x[o])
})
let user = { id: 2, name: 'Albert' }
let ex3 = () => {
    let a = undefined
    safeProp(user)('name').map(fp.first).map(value => { a = value })
    return a
}
console.log(ex3())




// 练习4：使用MayBe重写ex4，不要有if语句
// let ex4 = function (n) {
//     if (n) {
//         return parseInt(n)
//     }
// }
let ex4 = function (n) {
    let a = undefined
    MayBe.of(n).map(parseInt).map(value => { a = value })
    return a
}

console.log(ex4())




